package xyz.redtorch.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;

import xyz.redtorch.core.entity.OrderReq;
import xyz.redtorch.core.entity.SubscribeReq;
import xyz.redtorch.core.gateway.GatewaySetting;
import xyz.redtorch.web.annotation.Authorization;
import xyz.redtorch.web.controller.base.BaseController;
import xyz.redtorch.web.core.exception.ResourcesNotFoundException;
import xyz.redtorch.web.service.CoreEngineWebService;
import xyz.redtorch.web.core.vo.ResultVO;

import java.util.Optional;

@RestController
@RequestMapping("${api.base.path}/core")
public class CoreController extends BaseController {
	@Autowired
	private CoreEngineWebService coreEngineWebService;

	@RequestMapping(value = "/sendOrder", method = RequestMethod.POST)
//	@ResponseBody
	@Authorization
	public ResultVO sendOrder(@RequestHeader(name = "token") String token, @RequestBody OrderReq orderReq) {
		ResultVO result = new ResultVO();
		checkParams(orderReq, new String[]{"symbol", "priceType", "direction", "offset", "rtAccountID", "volume"});

		// Token作为OID
		orderReq.setOperatorID(token);

		String rtOrderID = coreEngineWebService.sendOrder(orderReq);

		result.setData(rtOrderID);

		return result;
	}

	@RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
//	@ResponseBody
	public ResultVO cancelOrder(@RequestBody JSONObject jsonObject) {
		ResultVO result = new ResultVO();
		checkParams(jsonObject, new String[]{"rtOrderID"});

		// 使用Token作为OID
		coreEngineWebService.cancelOrder(jsonObject.getString("rtOrderID"),jsonObject.getString("token"));
		return result;
	}

	@RequestMapping(value = "/cancelAllOrders", method = RequestMethod.POST)
//	@ResponseBody
	public ResultVO cancelAllOrders(@RequestBody JSONObject jsonObject) {
		ResultVO result = new ResultVO();
		coreEngineWebService.cancelAllOrders();
		return result;
	}

	@RequestMapping(value = "/subscribe", method = RequestMethod.POST)
//	@ResponseBody
	@Authorization
	public ResultVO subscribe(@RequestBody SubscribeReq subscribeReq) {
		ResultVO result = new ResultVO();
		Optional.ofNullable(subscribeReq).orElseThrow(new ResourcesNotFoundException("请求体参数不允许为空"));
		coreEngineWebService.subscribe(subscribeReq);
		return result;
	}

	@RequestMapping(value = "/unsubscribe", method = RequestMethod.POST)
//	@ResponseBody
	@Authorization
	public ResultVO unsubscribe(@RequestBody JSONObject jsonObject) {
		ResultVO result = new ResultVO();
		if (!coreEngineWebService.unsubscribe(jsonObject.getString("rtSymbol"), jsonObject.getString("gatewayID"))) {
			result.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
		}
		return result;
	}

	@RequestMapping(value = "/gateway", method = RequestMethod.PUT)
//	@ResponseBody
	@Authorization
	public ResultVO saveGatewaySettings(@RequestBody GatewaySetting gatewaySetting) {
		ResultVO result = new ResultVO();
		coreEngineWebService.saveOrUpdateGatewaySetting(gatewaySetting);
		return result;
	}

	@RequestMapping(path = "/gateways")
//	@ResponseBody
	@Authorization
	public ResultVO getGatewaySettings(String token) {
		ResultVO result = new ResultVO();
		result.setData(coreEngineWebService.getGatewaySettings());
		return result;
	}

	@RequestMapping(value = "/gateway", method = RequestMethod.DELETE)
//	@ResponseBody
	@Authorization
	public ResultVO deleteGatewaySettings(@RequestBody JSONObject jsonObject) {
		ResultVO result = new ResultVO();
		checkParams(jsonObject, new String[]{"gatewayID"});

		coreEngineWebService.deleteGateway(jsonObject.getString("gatewayID"));
		return result;
	}

	@RequestMapping(value = "/changeGatewayConnectStatus", method = RequestMethod.POST)
//	@ResponseBody
	@Authorization
	public ResultVO changeGatewayConnectStatus(@RequestBody JSONObject jsonObject) {
		ResultVO result = new ResultVO();
		checkParams(jsonObject, new String[]{"gatewayID"});

		coreEngineWebService.changeGatewayConnectStatus(jsonObject.getString("gatewayID"));
		return result;
	}

	@RequestMapping(path = "/accounts")
//	@ResponseBody
	@Authorization
	public ResultVO getAccounts(String token) {
		ResultVO result = new ResultVO();
		result.setData(coreEngineWebService.getAccounts());
		return result;
	}

	@RequestMapping(path = "/trades")
//	@ResponseBody
	@Authorization
	public ResultVO getTrades(String token) {
		ResultVO result = new ResultVO();
		result.setData(coreEngineWebService.getTrades());
		return result;
	}

	@RequestMapping(path = "/orders")
//	@ResponseBody
	@Authorization
	public ResultVO getOrders() {
		ResultVO result = new ResultVO();
		result.setData(coreEngineWebService.getOrders());
		return result;
	}

	@RequestMapping(path = "/localPositionDetails")
//	@ResponseBody
	@Authorization
	public ResultVO getLocalPositionDetails(String token) {
		ResultVO result = new ResultVO();
		result.setData(coreEngineWebService.getLocalPositionDetails());
		return result;
	}

	@RequestMapping(path = "/positions")
//	@ResponseBody
	@Authorization
	public ResultVO getPositions() {
		ResultVO result = new ResultVO();
		result.setData(coreEngineWebService.getPositions());
		return result;
	}

	@RequestMapping(path = "/contracts")
//	@ResponseBody
	@Authorization
	public ResultVO getContracts(String token) {
		ResultVO result = new ResultVO();
		result.setData(coreEngineWebService.getContracts());
		return result;
	}

	@RequestMapping(path = "/ticks")
//	@ResponseBody
	@Authorization
	public ResultVO getTicks(String token) {
		ResultVO result = new ResultVO();
		result.setData(coreEngineWebService.getTicks());
		return result;
	}

	@RequestMapping(path = "/logs")
//	@ResponseBody
	@Authorization
	public ResultVO getLogs(String token) {
		ResultVO result = new ResultVO();
		result.setData(coreEngineWebService.getLogDatas());
		return result;
	}

}
